﻿using System;
using System.Collections;
using System.Collections.Generic;
using Spine.Unity;
using UnityEngine;
using AnimationState = Spine.AnimationState;

public class SpineCard : MonoBehaviour
{
    /// <summary>
    /// _moPaiSkeletonGraphic
    /// </summary>
    private SkeletonGraphic _moPaiSkeletonGraphic;
    
    /// <summary>
    /// _shunZiSkeletonGraphic
    /// </summary>
    private SkeletonGraphic _shunZiSkeletonGraphic;
    
    /// <summary>
    /// _fasaguangGraphic
    /// </summary>
    private SkeletonGraphic _fasaguangGraphic;
    
    /// <summary>
    /// spine_moPai
    /// </summary>
    public Transform spine_moPai;

    /// <summary>
    /// spine_shunzi
    /// </summary>
    public Transform spine_shunzi;

    /// <summary>
    /// spine_fashaguang
    /// </summary>
    public Transform spine_fasaguang;
    
    /// <summary>
    /// mUnRegisterEventActions
    /// </summary>
    private List<Action> mUnRegisterEventActions = new List<Action>();


    void Awake()
    {
        _moPaiSkeletonGraphic = spine_moPai.GetComponent<SkeletonGraphic>();
        _shunZiSkeletonGraphic = spine_shunzi.GetComponent<SkeletonGraphic>();
        _fasaguangGraphic = spine_fasaguang.GetComponent<SkeletonGraphic>();
        spine_moPai.gameObject.SetActive(false);
        spine_shunzi.gameObject.SetActive(false);
        _fasaguangGraphic.gameObject.SetActive(false);
    }

    /// <summary>
    /// 摸牌动画
    /// </summary>
    public void ShowMoPai(Action action=null)
    {
        if (_moPaiSkeletonGraphic == null)
        {
            Debug.LogError("_moPaiSkeletonAnimation == null");
            return;
        }

        spine_moPai.gameObject.SetActive(true);
        _moPaiSkeletonGraphic.AnimationState.SetAnimation(0, "animation", false);
        AnimationState.TrackEntryDelegate ac = delegate
        {
            if (action != null)
            {
                action();
            }
            spine_moPai.gameObject.SetActive(false);
        };

        _moPaiSkeletonGraphic.AnimationState.Complete += ac;
        mUnRegisterEventActions.Add(() =>
        {
            _moPaiSkeletonGraphic.AnimationState.Complete -= ac;
        });
    }
    
    /// <summary>
    /// 展示顺子动画
    /// </summary>
    public void ShowShunZiAnimation(Action action = null)
    {
        if (_shunZiSkeletonGraphic == null)
        {
            Debug.LogError("_shunZiSkeletonGraphic == null");
            return;
        }
        
        spine_shunzi.gameObject.SetActive(true);
        _shunZiSkeletonGraphic.AnimationState.SetAnimation(0, "animation", false);
        AnimationState.TrackEntryDelegate ac = delegate
        {
            if (action != null)
            {
                spine_shunzi.gameObject.SetActive(false);
                ShowfasaGuangAnimation(action);
            }
        };

        _shunZiSkeletonGraphic.AnimationState.Complete += ac;
        mUnRegisterEventActions.Add(() =>
        {
            _shunZiSkeletonGraphic.AnimationState.Complete -= ac;
        });
    }
    
    /// <summary>
    /// 展示发散光动画
    /// </summary>
    public void ShowfasaGuangAnimation(Action action = null)
    {
        if (_fasaguangGraphic == null)
        {
            Debug.LogError("_fasaguangGraphic == null");
            return;
        }
        
        _fasaguangGraphic.gameObject.SetActive(true);
        _fasaguangGraphic.AnimationState.SetAnimation(0, "animation", false);
        AnimationState.TrackEntryDelegate ac = delegate
        {
            if (action != null)
            {
                action();
            }
        };

        _fasaguangGraphic.AnimationState.Complete += ac;
        mUnRegisterEventActions.Add(() =>
        {
            _fasaguangGraphic.AnimationState.Complete -= ac;
        });
    }




    /// <summary>
    /// 移除所有回调事件
    /// </summary>
    public void UnRegisterAll()
    {
        mUnRegisterEventActions.ForEach(action => action());
        mUnRegisterEventActions.Clear();
    }

    /// <summary>
    /// OnDestroy
    /// </summary>
    private void OnDestroy()
    {
        UnRegisterAll();
    }
}